﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telecom.Data;
using Telecom.Service;
using TelecomGis.Helper.Control;
using TelecomGis.Helper.Helper;

namespace TelecomGis.Controllers
{
    [Authorize]
    public class HomeController : ContronllerBase
    {

        private readonly MainmenuSer _IMainMenu = new MainmenuSer();
        public readonly MenuSer _IMenus = new MenuSer();
        public readonly UserSer _IUser = new UserSer();
        public readonly rolesMenuSer _IRoles_Menu = new rolesMenuSer();
        private readonly dauNoiTbSer _dauNoiTbSer = new dauNoiTbSer();

        //
        // GET: /Home/

        public ActionResult Index()
        {
            int CountDauNoi = 0;
            int DauNoiTBFirst = 0;
            var List = _dauNoiTbSer.list_OLT_TOQL_DONVI(UtContextgis._UserPageContext.ToKtByuser, UtContextgis._UserPageContext.DonViByUser, "PON");
            if (List != null)
            {

                 CountDauNoi  = List.Count();
                 try
                 {
                     DauNoiTBFirst = List.First().THIETBIID;
                 }
                 catch (Exception e) { DauNoiTBFirst = 0; }
            }

            ViewBag.coutIteDauNoi = CountDauNoi;
            ViewBag.DauNoiFirst = DauNoiTBFirst;

            return View();
        }

        public ActionResult LoadViewGianDoGpon_Home(string DauNoid)
        {
            ViewBag.NameDauNoi = _dauNoiTbSer.getByid(long.Parse(DauNoid)).TEN;
            modelGianDoGPOn model = new modelGianDoGPOn();
            model.DauNoiTBId = int.Parse(DauNoid);
            return PartialView("_LoadViewGianDoGpon_Home", model);
        }
           

        public ActionResult menuPage()
        {
            ViewBag.USER = UtContextgis._UserPageContext.users.USERNAME;

            // tìm roles cho các user
            var rolesForUser = _IUser.GetIdRolesForUser(UtContextgis._UserPageContext.users.USERNAME);
            //Tìm main menu là user có quyền
            var list_enity = _IMainMenu.getListMainMenuForUser(rolesForUser);

            ViewBag.NameUserLogin = UtContextgis._UserPageContext.users.HOTEN;

            return PartialView("_menuPage", list_enity);
        }

        public ActionResult MenuChildOnUser(int menuId)
        {
            List<MENUS> _listMenu = new List<MENUS>();

            var rolesForUser = _IUser.GetIdRolesForUser(UtContextgis._UserPageContext.users.USERNAME);

            _listMenu = _IRoles_Menu.ListMenuOnRolesArrayAndMainMenu(rolesForUser, menuId);

            return PartialView("_MenuChildOnUser", _listMenu);
        }

        [HttpPost]
        public ActionResult UploadFile(string qqfile)
        {
            try
            {
                qqfile = qqfile.Replace(",", "");
                string basePath = "~/Content/fileUpload" ;
                FileHelper fileHelper = new FileHelper(basePath);
                string[] fileNameArray = qqfile.Split(new char[] { '.' });
                if (fileNameArray.Length == 1)
                {
                    return Json(new { status = "error", _path = "" }, JsonRequestBehavior.AllowGet);
                }

                bool checkFiles = fileHelper.checkFileExits(qqfile);

                string fileName = fileHelper.SaveFile(null, qqfile, Request.InputStream);
                //string filePathStr = Url.Content(basePath) + @"/" + upl_file_add_tbs.FileName;

                if (checkFiles == true)
                {
                    return Json(new { status = "success", _path = fileName, _name = fileName }, JsonRequestBehavior.AllowGet);
                    //return Json(new { status = "success", _path = _urlReturn, _size = Request.InputStream.Length }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { status = "success", _path = fileName, _name = fileName }, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return Json(new { status = "error", _path = "", _name = "" }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult DeleFile(string file)
        {
            try
            {
                string basePath = "~/Content/fileUpload";
                FileHelper fileHelper = new FileHelper(basePath);
                fileHelper.DeleteFile(file);
                return Json(new { status = "Success" }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { status = "Error" }, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
